    //
//  Three20Additions.m
//  MClient
//
//  Created by  史正烨 on 11-2-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Three20Additions.h"
#import "MCUserDefaults.h"
#import "UIColor-Expanded.h"

@implementation TTDefaultStyleSheet(MCLientAddition)

- (UIColor*) tabTintColor {
	if ([MCUserDefaults instance].skin_index == 1) {
		return [UIColor colorWithR:36 withG:151 withB:94];
	} else if ([MCUserDefaults instance].skin_index == 2) {
		return [UIColor colorWithR:0 withG:130 withB:110];
	} else {
		return [UIColor colorWithR:12 withG:121 withB:217];
	}
}

- (UIColor*) tabBarTintColor {
	if ([MCUserDefaults instance].skin_index == 1) {
		return [UIColor colorWithR:28 withG:156 withB:78];
	} else if ([MCUserDefaults instance].skin_index == 2) {
		return [UIColor colorWithR:43 withG:101 withB:97];
	} else {
		return [UIColor colorWithR:7 withG:80 withB:205];
	}
}

- (UIColor*) linkTextColor {
	if ([MCUserDefaults instance].skin_index == 1) {
		return [UIColor whiteColor];
	} else if ([MCUserDefaults instance].skin_index == 2) {
		return [UIColor whiteColor];
	} else {
		return [UIColor whiteColor];
	}
}

- (UIColor*) tabNormalTextColor {
	if ([MCUserDefaults instance].skin_index == 1) {
		return [UIColor blackColor];
	} else if ([MCUserDefaults instance].skin_index == 2) {
		return [UIColor blackColor];
	} else {
		return [UIColor blackColor];
	}
}

- (TTStyle*)tabStrip {
	UIColor* border = [TTSTYLEVAR(tabTintColor) multiplyHue:0 saturation:0 value:0.4];
	UIColor* color1 = nil;
	UIColor* color2 = nil;
	if ([MCUserDefaults instance].skin_index == 1) {
		color1 = [UIColor colorWithR:167 withG:223 withB:198];
		color2 = [UIColor colorWithR:138 withG:213 withB:180];
	} else if ([MCUserDefaults instance].skin_index == 2) {
		color1 = [UIColor colorWithR:116 withG:186 withB:183];
		color2 = [UIColor colorWithR:83 withG:166 withB:162];
	} else {
		color1 = [UIColor colorWithR:0xAE withG:0xDE withB:0xFF];
		color2 = [UIColor colorWithR:0x8C withG:0xD1 withB:0xFF];
	}
	return
    //[TTReflectiveFillStyle styleWithColor:TTSTYLEVAR(tabTintColor) next:
	//[TTSolidFillStyle styleWithColor:[UIColor colorWithR:0xAE withG:0xDE withB:0xFF] next:
	[TTLinearGradientFillStyle styleWithColor1:color1 color2:color2 next:
	 [TTFourBorderStyle styleWithTop:nil right:nil bottom:border left:nil width:1 next:nil]];
}

- (TTStyle*)tabGridTab:(UIControlState)state corner:(short)corner {
	TTShape* shape = nil;
	if (corner == 1) {
		shape = [TTRoundedRectangleShape shapeWithTopLeft:8 topRight:0 bottomRight:0 bottomLeft:0];
	} else if (corner == 2) {
		shape = [TTRoundedRectangleShape shapeWithTopLeft:0 topRight:8 bottomRight:0 bottomLeft:0];
	} else if (corner == 3) {
		shape = [TTRoundedRectangleShape shapeWithTopLeft:0 topRight:0 bottomRight:8 bottomLeft:0];
	} else if (corner == 4) {
		shape = [TTRoundedRectangleShape shapeWithTopLeft:0 topRight:0 bottomRight:0 bottomLeft:8];
	} else if (corner == 5) {
		shape = [TTRoundedRectangleShape shapeWithTopLeft:8 topRight:0 bottomRight:0 bottomLeft:8];
	} else if (corner == 6) {
		shape = [TTRoundedRectangleShape shapeWithTopLeft:0 topRight:8 bottomRight:8 bottomLeft:0];
	} else {
		shape = [TTRectangleShape shape];
	}
	
	UIColor* highlight = RGBACOLOR(255, 255, 255, 0.7);
	UIColor* shadow = [TTSTYLEVAR(tabTintColor) multiplyHue:1 saturation:1.1 value:0.88];
	
	if (state == UIControlStateSelected) {
		return
		[TTShapeStyle styleWithShape:shape next:
		 [TTSolidFillStyle styleWithColor:RGBCOLOR(150, 168, 191) next:
		  [TTInnerShadowStyle styleWithColor:RGBACOLOR(0,0,0,0.6) blur:3 offset:CGSizeMake(0, 0) next:
		   [TTBoxStyle styleWithPadding:UIEdgeInsetsMake(11, 10, 9, 10) next:
			[TTPartStyle styleWithName:@"image" style:[self tabGridTabImage:state] next:
			 [TTTextStyle styleWithFont:[UIFont systemFontOfSize:14]  color:RGBCOLOR(255, 255, 255)
						minimumFontSize:10 shadowColor:RGBACOLOR(0,0,0,0.1) shadowOffset:CGSizeMake(-1,-1)
								   next:nil]]]]]];
	} else {
		return
		[TTShapeStyle styleWithShape:shape next:
		 [TTBevelBorderStyle styleWithHighlight:highlight shadow:shadow width:1 lightSource:125 next:
		  [TTBoxStyle styleWithPadding:UIEdgeInsetsMake(11, 10, 9, 10) next:
		   [TTPartStyle styleWithName:@"image" style:[self tabGridTabImage:state] next:
			[TTTextStyle styleWithFont:[UIFont systemFontOfSize:14]  color:self.linkTextColor
					   minimumFontSize:10 shadowColor:[UIColor clearColor]//[UIColor colorWithWhite:255 alpha:0.9]
						  shadowOffset:CGSizeMake(0, -1) next:nil]]]]];
	}
}

- (TTStyle*)tabRound:(UIControlState)state {
	if (state == UIControlStateSelected) {
		return
		[TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:TT_ROUNDED] next:
		 [TTInsetStyle styleWithInset:UIEdgeInsetsMake(9, 1, 8, 1) next:
		  [TTShadowStyle styleWithColor:RGBACOLOR(255,255,255,0.8) blur:0 offset:CGSizeMake(0, 1) next:
		   [TTReflectiveFillStyle styleWithColor:TTSTYLEVAR(tabBarTintColor) next:
			[TTInnerShadowStyle styleWithColor:RGBACOLOR(0,0,0,0.3) blur:1 offset:CGSizeMake(1, 1) next:
			 [TTInsetStyle styleWithInset:UIEdgeInsetsMake(-1, -1, -1, -1) next:
			  [TTBoxStyle styleWithPadding:UIEdgeInsetsMake(0, 10, 0, 10) next:
			   [TTTextStyle styleWithFont:[UIFont boldSystemFontOfSize:14]  color:[UIColor whiteColor]
						  minimumFontSize:8 shadowColor:[UIColor colorWithWhite:0 alpha:0.5]
							 shadowOffset:CGSizeMake(0, -1) next:nil]]]]]]]];
	} else {
		return
		[TTBoxStyle styleWithPadding:UIEdgeInsetsMake(0, 10, 0, 10) next:
		 [TTTextStyle styleWithFont:[UIFont boldSystemFontOfSize:16]  color:self.tabNormalTextColor
					minimumFontSize:8 shadowColor:[UIColor colorWithWhite:1 alpha:0.9]
					   shadowOffset:CGSizeMake(0, -1) next:nil]];
	}
}

@end

@implementation TTReflectiveFillStyle(MCLientAddition)
// TTStyle

- (void)draw:(TTStyleContext*)context {
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGRect rect = context.frame;
	
	CGContextSaveGState(ctx);
	[context.shape addToPath:rect];
	CGContextClip(ctx);
	
	[_color setFill];
	CGContextFillRect(ctx, rect);
	
	// XXjoe These numbers are totally biased towards the colors I tested with.  I need to figure out
	// a formula that works well for all colors
//	UIColor* lighter = nil, *darker = nil;
//	if (_color.value < 0.5) {
//		lighter = HSVCOLOR(_color.hue, ZEROLIMIT(_color.saturation-0.5), ZEROLIMIT(_color.value+0.25));
//		darker = HSVCOLOR(_color.hue, ZEROLIMIT(_color.saturation-0.1), ZEROLIMIT(_color.value+0.1));
//	} else if (_color.saturation > 0.6) {
//		lighter = HSVCOLOR(_color.hue, _color.saturation*0.3, _color.value*1);
//		darker = HSVCOLOR(_color.hue, _color.saturation*0.9, _color.value+0.05);
//	} else {
//		lighter = HSVCOLOR(_color.hue, _color.saturation*0.4, _color.value*1.2);
//		darker = HSVCOLOR(_color.hue, _color.saturation*0.9, _color.value+0.05);
//	}
	UIColor* lighter = [_color multiplyHue:1 saturation:0.5 value:1.35];
	UIColor* darker = [_color multiplyHue:1 saturation:0.88 value:1.05];
	UIColor* colors[] = {lighter, darker};
	
	CGGradientRef gradient = [self newGradientWithColors:colors count:2];
	CGContextDrawLinearGradient(ctx, gradient, CGPointMake(rect.origin.x, rect.origin.y),
								CGPointMake(rect.origin.x, rect.origin.y+rect.size.height*0.5),
								kCGGradientDrawsBeforeStartLocation);
	CGGradientRelease(gradient);
	
	CGContextRestoreGState(ctx);
	
	return [self.next draw:context];
}

@end

@implementation TTStyleSheet(MCLientAddition)
- (TTStyle*)styleWithSelector:(NSString*)selector forState:(UIControlState)state {
	NSString* key = state == UIControlStateNormal
    ? selector
    : [NSString stringWithFormat:@"%@%d", selector, state];
	TTStyle* style = [_styles objectForKey:key];
	if (!style) {
		SEL sel = NSSelectorFromString(selector);
		if ([self respondsToSelector:sel]) {
			style = [self performSelector:sel withObject:(id)state];
			if (style) {
				if (!_styles) {
					_styles = [[NSMutableDictionary alloc] init];
				}
				//[_styles setObject:style forKey:key];
			}
		}
	}
	return style;
}
@end